
from rest_framework import serializers
from goods_info.models import UnitsModel

class UnitsSerializer(serializers.ModelSerializer):
    """
        计量单位的序列化器，和反序列化
    """

    # 增加一个新属性，把基本单位和副单位结合在一起
    units_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = UnitsModel
        fields = '__all__'

    def get_units_name(self, obj):
        # obj是指当前模型对象，就是UnitsModel
        return str(obj)   # 就是调用该对象的__str__函数
